package stream;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.comparator.ComparatorChain;
import cn.hutool.core.comparator.PropertyComparator;

import java.util.Comparator;
import java.util.List;

public class SortTest {
    public static void main(String[] args) {
        List<Student> studentList = CollUtil.toList(new Student().setId(1).setAge(10), new Student().setId(2).setAge(9), new Student().setId(3).setAge(11));

        studentList.stream().sorted((student1, student2) -> student1.getAge() - student2.getAge()).forEach(System.out::println);
        studentList.stream().sorted(Comparator.comparingInt(Student::getAge).reversed().thenComparingInt(Student::getId).reversed()).forEach(System.out::println);
        studentList.stream().sorted(new PropertyComparator<Student>("age").reversed()).forEach(System.out::println);

        ComparatorChain<Student> comparators = new ComparatorChain<>();
        comparators.addComparator(new PropertyComparator<Student>("age").reversed());
        comparators.addComparator(new PropertyComparator<Student>("id").reversed());
        studentList.stream().sorted(comparators).forEach(System.out::println);
    }
}
